var _controller;

function editSaveInit(controller) {
    this._controller = controller;

    //新增 修改
    $('#saveform').click(function () {
        //表单验证
        if (!beforeSubmit()) {
            return false;
        }
        showInfoAlertSelect("你确定要提交当前记录吗?", saveFunc);
    });
}

function saveFunc() {

    //提交按钮禁用
    if (this._controller == 'schedule' || this._controller == 'carcheckout') {
        $("#saveform_before").attr("disabled", "disabled");
        countDown('saveform_before', 9, 1000);
    } else {
        $("#saveform").attr("disabled", "disabled");
        countDown('saveform', 9, 1000);
    }

    $.ajax({
        url: "/" + this._controller + "/saveEdit",
        type: "POST",
        data: $('#myform').serializeArray(),
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode == "1") {
                var url = $('input[name="redirect_url"]').val();
                if (url != undefined && url != "") {
                    showInfoAlert(decodeURI(r.returnMessage), gotoRedirectUrl);
                } else {
                    showInfoAlert(decodeURI(r.returnMessage), jumpToIndex);
                }
            } else {
                //特殊业务：如主表保存成功，从表保存失败时会走这个方法
                if (r.id != undefined) {
                    $("#id").val(r.id);
                }
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

//x秒后重新提交
function countDown(btnName, seconds, speed) {
    var txt = "数据正在保存中，请稍后...（" + seconds + "）";
    $('#' + btnName).text(txt);

    if (seconds === 0) {
        $('#' + btnName).text("重新提交");
        $('#' + btnName).removeAttr("disabled");
        return;
    }

    var newSeconds = parseInt(seconds) - 1;
    setTimeout("countDown('" + btnName + "'," + newSeconds + ",  " + speed + ")", speed);
}

//跳转
function jumpToIndex() {
    window.location = "/" + this._controller + "/index";
}

// 根据公司ID自动获联系人及电话
function getLinkPeople(id, linkPeople, linkPeopleCn, linkMobile, openBack, backAccount) {

    //非空判断
    if (id == "" || id == undefined || id <= 0) {
        return false;
    }

    //mark: this is a standard!
    $.ajax({
        url: "/basic/company/getLinkPeopleAndMobile",
        type: "POST",
        data: {companyid: id},
        dataType: 'json',
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            if (data.returnCode === 1) {
                $("input[name='" + linkPeople + "']").val(data.linkName);
                $("input[name='" + linkPeopleCn + "']").val(data.linkNameCn);
                $("input[name='" + linkMobile + "']").val(data.linkMobile);

                if (openBack != null && openBack != '') {
                    $("input[name='" + openBack + "']").val(data.openBack);
                    $("input[name='" + backAccount + "']").val(data.backAccount);
                }
            }
        }
    });
}